package com.zhu.gmallpublisher.controller;


import com.zhu.gmallpublisher.bean.UserChangeCtPerType;
import com.zhu.gmallpublisher.bean.UserPageCt;
import com.zhu.gmallpublisher.bean.UserTradeCt;
import com.zhu.gmallpublisher.service.UserStatsService;
import com.zhu.gmallpublisher.utils.GetDateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 用户主题控制层
 */

@RestController
@RequestMapping("/zhu/gmall/sugar/user")
public class UserStatsController {


    @Autowired
    private UserStatsService userStatsService;

    @RequestMapping("/userChangeCt")
    public String getUserChange(@RequestParam(value = "date",defaultValue = "0")Integer selectDate){
        if( selectDate == 0){
            selectDate = GetDateUtil.getCurrentTime();
        }
        List<UserChangeCtPerType> userChangeCtList = userStatsService.getUserChangeCt(selectDate);
        if(userChangeCtList == null || userChangeCtList.size() == 0){
            return "";
        }
        StringBuilder resultJson = new StringBuilder("[");
        for (int i = 0; i < userChangeCtList.size(); i++) {
            UserChangeCtPerType userChangeCtPerType = userChangeCtList.get(i);
            String type = userChangeCtPerType.getType();
            String userCt = userChangeCtPerType.getUserCt();
            resultJson.append(
                    "{\n" +
                            "\t\"type\": \"" + type + "\",\n" +
                            "\t\"user_ct\": \"" + userCt + "\"\n" +
                            "}");
            if(i < userChangeCtList.size()-1){
                resultJson.append(",");
            }else {
                resultJson.append("]");
            }
        }  //[{},{},{}]
        return "{\n" +
                "  \"status\": 0,\n" +
                "  \"msg\": \"\",\n" +
                "  \"data\": {\n" +
                "    \"columns\": [\n" +
                "      {\n" +
                "        \"name\": \"变动类型\",\n" +
                "        \"id\": \"type\"\n" +
                "      },\n" +
                "      {\n" +
                "        \"name\": \"用户数\",\n" +
                "        \"id\": \"user_ct\"\n" +
                "      }\n" +
                "    ],\n" +
                "    \"rows\": " + resultJson + "\n" +
                "  }\n" +
                "}";

    }

    //用户行为漏斗分析
    @RequestMapping("/pageCt")
    public String getUserPageCt(@RequestParam(value = "date",defaultValue = "0") Integer selectDate){
        if(selectDate == 0){
            selectDate = GetDateUtil.getCurrentTime();
        }
        List<UserPageCt> userPageCtList = userStatsService.getUserPageCt(selectDate);
        if(userPageCtList == null || userPageCtList.size() == 0){
            return "";
        }
        StringBuilder dataResultJsonString = new StringBuilder("[");
        for (int i = 0; i < userPageCtList.size(); i++) {
            UserPageCt userPageCt = userPageCtList.get(i);
            String pageId = userPageCt.getPageId();
            Integer uvCt = userPageCt.getUvCt();

            dataResultJsonString.append("{\n" +
                    "        \"page_id\": \"" + pageId + "\",\n" +
                    "        \"uv_ct\": \"" + uvCt + "\"\n" +
                    "      }"
            );
            if( i < userPageCtList.size()-1){
                dataResultJsonString.append(",");
            }else {
                dataResultJsonString.append("]");
            }
        }
        return "{\n" +
                "  \"status\": 0,\n" +
                "  \"msg\": \"\",\n" +
                "  \"data\": {\n" +
                "    \"columns\": [\n" +
                "      {\n" +
                "        \"name\": \"页面\",\n" +
                "        \"id\": \"page_id\"\n" +
                "      },\n" +
                "      {\n" +
                "        \"name\": \"独立访客数\",\n" +
                "        \"id\": \"uv_ct\"\n" +
                "      }\n" +
                "    ],\n" +
                "    \"rows\": " + dataResultJsonString + "\n" +
                "  }\n" +
                "}";

    }

    //新增交易和下单用户
    @RequestMapping("/userCt")
    public String getTradeUserCt(@RequestParam(value = "date",defaultValue = "0") Integer selectDate){
        if(selectDate == 0){
            selectDate = GetDateUtil.getCurrentTime();
        }
        List<UserTradeCt> tradeUserCtList = userStatsService.getTradeUserCt(selectDate);
        if(tradeUserCtList == null || tradeUserCtList.size() == 0){
            return "";
        }
        StringBuilder resultJsonString = new StringBuilder("[");
        for (int i = 0; i < tradeUserCtList.size(); i++ ) {
            UserTradeCt userTradeCt = tradeUserCtList.get(i);
            String tradeCtType = userTradeCt.getType();
            Integer userCount = userTradeCt.getUserCount();
            resultJsonString
                    .append("{\n" + "\t\"type\": \"").append(tradeCtType)
                    .append("\",\n").append("\t\"user_ct\": \"").append(userCount)
                    .append("\"\n").append("}");
            if(i < tradeUserCtList.size() - 1){
                resultJsonString.append(",");
            }else {
                resultJsonString.append("]");
            }
        }

        return "{\n" +
                "  \"status\": 0,\n" +
                "  \"msg\": \"\",\n" +
                "  \"data\": {\n" +
                "    \"columns\": [\n" +
                "      {\n" +
                "        \"name\": \"交易类型\",\n" +
                "        \"id\": \"type\"\n" +
                "      },\n" +
                "      {\n" +
                "        \"name\": \"新增用户数\",\n" +
                "        \"id\": \"user_ct\"\n" +
                "      }\n" +
                "    ],\n" +
                "    \"rows\": " + resultJsonString +
                "  }\n" +
                "}";
    }

}
